define([ "dojo/_base/declare", "gistech/widgets/baseWidget/BaseWidget",
    "dojo/text!./templates/Template.html","dojo/_base/fx","dojo/fx","dojo/fx/easing", "dojo/dom",
    "dojo/on","dojo/aspect","dojo/query","dijit/layout/ContentPane","dijit/layout/TabContainer","dojo/parser",
    "dojo/topic","dojo/_base/lang","dijit/layout/TabContainer","dojo/dom-style"
    ],
    function(declare,BaseWidget, template,baseFx,fx,easing,Dom,on,aspect,query,ContentPane,TabContainer,Parser,
    			Topic,lang,TabContainer,style) {
        return declare([ BaseWidget], {
        	//模板页面
            templateString : template,
            tabCom:null,
            hasExpand:false,//true:展开，flase:没展开
 
            panelWidth:495,
            relatedom:null,
 
            defaultExpand:false,
            openRightWidget:0,
            constructor : function() {
            	this.inherited(arguments);
            },
            //组件初始化方法
            postCreate : function() {
            	this.panelWidth = this.domNode.offsetWidth;
            	this.tabCom=new ContentPane({region: 'center', tabPosition: 'top',useSlider:false});
            	this.tabCom.placeAt(this.tab).startup();
            	
            	on(this.expandBtn,"click",dojo.hitch(this,this.expandLeft));
            	Topic.subscribe("removealltab", lang.hitch(this, this.removeAllTab));
            	Topic.subscribe("expandLeft", lang.hitch(this, this.expandLeft));
            	Topic.subscribe("closeTab", lang.hitch(this, this.closeTab));
            	this.inherited(arguments);
            },
            startup:function(){
            	this.inherited(arguments);
            },
            addTab:function(widget,title,closable,onClose){
            	//debugger;
            	widget.enable = false;
            	widget.title=title;/////
            	this.tabCom.addChild(widget);
            	this._setLeftTabCss();
            },
            removeTab:function(widget){
        		this.tabCom.removeChild(widget);
        		//this.setWidth();
            },
            removeAllTab:function(){
            	//debugger
            	var tabs=this.tabCom.getChildren();
            	console.log(tabs)
            	var temtabs = this.tabCom;
            	//debugger
            	dojo.forEach(tabs,function(item){
            			item.onClose();
            			temtabs.removeChild(item);
            			item.destroy();
            	});
            	this.setWidgetWidth(495);
            },
            _setLeftTabCss:function(){
    			//set tab css
            	$('.dijitTabListWrapper >div',Global.widgets.left.domNode).css("width","inherit");
            	$('.dijitTabListWrapper >div>:nth-child(1)',Global.widgets.left.domNode).css({"width":"50%","float":"left"});
            	$('.dijitTabListWrapper >div>:nth-child(2)',Global.widgets.left.domNode).css({"width":"50%"});
    		},
            setWidgetWidth:function(widthvar){
            	  this.panelWidth = widthvar;
            	  var rightwidget = dojo.query(".rightWidgetRoot")[0];
                  style.set(rightwidget,"right","0px");
                  style.set(rightwidget,"width",widthvar+"px");
                  this.expandBtn.src = "images/unfold-Right.png";
                  this.hasExpand = true;
            },
            selectTab:function(widget){
            	if(this.tabCom.hasChildren(widget)){
            		this.tabCom.selectChild(widget);
            	}
            },
            getTab:function(title){
            	var tabs=this.tabCom.getChildren();
            	var finditem = null;
            	dojo.forEach(tabs,function(item){
            		if(item.title==title){
            			finditem = item;
            			return;
            		}
            	});
            	return finditem;
            },
            onClose:function(e){
            	return true;
            },
            closeTab:function(title){
            	this.setWidth();
            },
            expandLeft:function(evt){
            	debugger;
            	this.hasExpand=!this.hasExpand;
            	var anim=fx.combine([
					baseFx.animateProperty({
					    node: this.domNode,
					    properties: {
					        right: this.hasExpand?0:-this.panelWidth
					    }
					})
            	]);
            	aspect.after(anim, "onEnd", dojo.hitch(this,function(){
            		debugger
            		this.expandBtn.src=this.hasExpand?"images/unfold-Right.png":"images/fold-Right.png";
            		//Topic.publish('leftExpand',this.hasExpand);
            		if(this.hasExpand){
            			var width = $(this.domNode).width()
                		$(Global.widgets.BottomArrow2.domNode).css("marginRight",width);
                		$(Global.widgets.zoombar.domNode).css("marginRight",width);
                		if(Global.widgets.BottomArrow2.toolbox){
                			$(Global.widgets.BottomArrow2.toolbox.domNode).css("marginRight",width);
                		}
                		if(window.currentShowTemplateInitalPos && window.currentShowTemplateInitalPos.obj){
                			var right=parseInt($(window.currentShowTemplateInitalPos.obj).closest('.esriPopupVisible').css("right"));
                			$(window.currentShowTemplateInitalPos.obj).closest('.esriPopupVisible').css("right",(right+this.panelWidth)+"px");
                		}
            		}else{
            			$(Global.widgets.BottomArrow2.domNode).css("marginRight",0);
            			$(Global.widgets.zoombar.domNode).css("marginRight",0);
            			if(Global.widgets.BottomArrow2.toolbox){
            				$(Global.widgets.BottomArrow2.toolbox.domNode).css("marginRight",0);
            			}
            			if(window.currentShowTemplateInitalPos && window.currentShowTemplateInitalPos.obj){
                			var right=parseInt($(window.currentShowTemplateInitalPos.obj).closest('.esriPopupVisible').css("right"));
                			$(window.currentShowTemplateInitalPos.obj).closest('.esriPopupVisible').css("right",(right-this.panelWidth)+"px");
                		}
            		}
            	}));
            	this.openRightWidget=0;
            	anim.play();
            },
            destroy:function(){
            	this.inherited(arguments);
            }
        });
    });
